import {
	requestNoAuth,
	request
} from '../../lib/java110/java110Request.js';
import url from '../../constant/url.js';
import mapping from '../../lib/java110/java110Mapping.js'

import {
	isNull
} from '../../lib/java110/utils/StringUtil.js'


// #ifdef H5

const WexinPayFactory = require('../../lib/java110/factory/WexinPayFactory.js');

// #endif

/**
 *  下单
 */
export function saveStoreCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.saveStoreCart,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {

						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 *  更新购物车（尤其时下单商品数量）
 */
export function updateStoreCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.updateStoreCart,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {

						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}
/**
 *  删除购物车
 */
export function deleteStoreCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.deleteStoreCart,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}
/**
 *  查询购物车
 */
export function getStoreCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.queryStoreCart,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					let _json = res.data;
					if (_json.code == 0) {
						resolve(_json);
						return;
					}
					reject(_json.msg);
				},
				fail: function(e) {
					reject();
				}
			});
		})
}



/**
 * 商品统一下单 
 * @param {Object} dataObj 下单报文
 */
export function goodsUnifieldOrder(dataObj) {

	return new Promise(
		(resolve, reject) => {
			request({
				url: url.goodsUnifieldOrder,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject(res);
				},
				fail: function(e) {
					reject(e);
				}
			});
		})
}

/**
 * 发起微信支付
 * add by 吴学文 2020-11-12
 * @param {Object} data 微信支付参数
 */
export function toPay(data) {
	return new Promise((resolve, reject) => {
		let obj = {};
		let orderInfo = {};
		//4.0 判断磁盘是否存在

		let mallFrom = uni.getStorageSync(mapping.MALL_FROM)

		// #ifdef H5
		WexinPayFactory.wexinPay(data, function() {
			uni.showToast({
				title: "支付成功",
				duration: 2000
			});
			uni.navigateTo({
				url: '/pages/order/order?stat=2002'
			});
		});
		// #endif
		// #ifdef MP-WEIXIN
		uni.requestPayment({
			'timeStamp': data.timeStamp,
			'nonceStr': data.nonceStr,
			'package': data.package,
			'signType': data.signType,
			'paySign': data.sign,
			'success': function(res) {
				uni.navigateTo({
					url: '/pages/order/order?stat=2002'
				})
			},
			'fail': function(res) {
				console.log('fail:' + JSON.stringify(res));
			}
		});
		// #endif
	})
}

/**
 * 发起微信支付
 * add by 吴学文 2020-11-12
 * @param {Object} data 微信支付参数
 */
export function toPayServ(data, _url) {
	return new Promise((resolve, reject) => {
		let obj = {};
		let orderInfo = {};
		//4.0 判断磁盘是否存在
		if (!_url) {
			_url = '/pages/homemaking/order?stat=2102';
		}
		
		let mallFrom = uni.getStorageSync(mapping.MALL_FROM)
		
		// #ifdef H5
		WexinPayFactory.wexinPay(data, function() {
			uni.showToast({
				title: "支付成功",
				duration: 2000
			});
			uni.navigateTo({
				url: _url
			});
		});
		// #endif
		// #ifdef MP-WEIXIN
		uni.requestPayment({
			'timeStamp': data.timeStamp,
			'nonceStr': data.nonceStr,
			'package': data.package,
			'signType': data.signType,
			'paySign': data.sign,
			'success': function(res) {
				uni.navigateTo({
					url: _url
				})
			},
			'fail': function(res) {
				console.log('fail:' + JSON.stringify(res));
			}
		});
		// #endif
	})
}

export function getOrderState() {
	return [{
			state: 1001,
			text: '全部订单',
		},
		{
			state: 2002,
			text: '待发货',
		},
		{
			state: 4004,
			text: '待评价',
		},
		{
			state: 5005,
			text: '退款',
		}
	];
}

export function getHomemakingOrderState() {
	return [{
			state: 1101,
			text: '待下单',
		},
		{
			state: 2102,
			text: '待接单',
		},
		{
			state: 3103,
			text: '待上门',
		},
		{
			state: 3104,
			text: '待支付',
		},
		{
			state: 4004,
			text: '待评价',
		},
		{
			state: 8008,
			text: '已完成',
		}
	];
}

/**
 *  查询订单  (查询已经生成订单)
 */
export function queryStoreOrderCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.queryStoreOrderCart,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {

						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

export function expeditingDelivery(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.expeditingDelivery,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

export function receiveStoreOrderCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.receiveStoreOrderCart,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 *  下单
 */
export function returnStoreOrderCart(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.saveStoreOrderCartReturn,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.data.code == 0) {

						resolve(res.data);
						return;
					}
					reject(res.data.msg);
				},
				fail: function(e) {
					reject(e);
				}
			});
		})
}

/**
 *  查询购物车费用
 */
export function getStoreOrderCartFees(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.queryStoreOrderCartFee,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 * 发起微信支付
 * add by 吴学文 2020-11-12
 * @param {Object} dataObj 微信支付参数
 */
export function toServFeePay(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.toQrServFeePay,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject(res);
				},
				fail: function(e) {
					reject();
				}
			});
		})
}


/**
 *  查询购物车
 */
export function computeStoreOrderPrice(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.computeStoreOrderPrice,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 *  查询购物车
 */
export function computeGroupProductPrice(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.computeGroupProductPrice,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 *  计算秒杀商品价格
 */
export function computeSkillProductPrice(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.computeSkillProductPrice,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.statusCode == 200) {
						resolve(res.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 * 微信 支付
 * @param {*} _data 
 */
export function payWechat(_data, _successUrl) {
	if (!_successUrl) {
		_successUrl = '/pages/order/order?stat=2002';
	}
	wx.showLoading({
		title: '支付中'
	});
	request({
		url: url.unifiedPayment,
		method: "POST",
		data: _data,
		//动态数据
		success: function(res) {
			wx.hideLoading();
			if (res.data.code == '0') {
				let data = res.data; //成功情况下跳转
				// #ifdef MP-WEIXIN
				uni.requestPayment({
					'timeStamp': data.timeStamp,
					'nonceStr': data.nonceStr,
					'package': data.package,
					'signType': data.signType,
					'paySign': data.sign,
					'success': function(res) {
						uni.navigateTo({
							url: _successUrl
						})
					},
					'fail': function(res) {
						console.log('fail:' + JSON.stringify(res));
					}
				});
				// #endif
				// #ifdef H5
				WexinPayFactory.wexinPay(data, function() {
					uni.navigateTo({
						url: _successUrl
					})
				});
				// #endif

				return;
			}
			if (res.data.code == '100') {
				let data = res.data; //成功情况下跳转
				uni.showToast({
					title: res.data.msg,
					duration: 2000
				});
				setTimeout(function() {
					uni.navigateTo({
						url: _successUrl
					})
				}, 2000)
				return;
			}
			wx.showToast({
				title: "支付失败",
				icon: 'none',
				duration: 2000
			});
		},
		fail: function(e) {
			wx.hideLoading();
			wx.showToast({
				title: "服务器异常了",
				icon: 'none',
				duration: 2000
			});
		}
	});
}


/**
 *  查询购物车费用
 */
export function getMyProductGroupOrder(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.listMyProductGroupOrder,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.data.code == 0) {
						resolve(res.data.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 * 申请拼团退款
 * add by 吴学文 2020-11-12
 * @param {Object} dataObj 微信支付参数
 */
export function applyProductGroupOrderReturn(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.applyProductGroupOrderReturn,
				method: "POST",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.data.code == 0) {
						resolve(res.data);
						return;
					}
					reject(res);
				},
				fail: function(e) {
					reject();
				}
			});
		})
}


//查询二维码支付
export function getQrCodeData(_objData) {
	return new Promise((resolve, reject) => {
		requestNoAuth({
			url: url.getNativeQrcodePayment,
			method: "GET",
			data: _objData, //动态数据
			success: function(res) {
				if (res.statusCode == 200) {
					resolve(res.data);
					return;
				}
				reject();
			},
			fail: function(e) {
				reject();
			}
		});
	})
}


/**
 *  查询物流详情
 */
export function getLogisticsDetail(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.queryLogisticsDetail,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.data.code == 0) {
						resolve(res.data.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 *  查询物流详情
 */
export function getStoreOrderWriteOff(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.queryStoreOrderWriteOff,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.data.code == 0) {
						resolve(res.data.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

/**
 *  秒杀订单
 */
export function listProductSeckillOrder(dataObj) {
	return new Promise(
		(resolve, reject) => {
			request({
				url: url.listProductSeckillOrder,
				method: "GET",
				data: dataObj,
				//动态数据
				success: function(res) {
					if (res.data.code == 0) {
						resolve(res.data.data);
						return;
					}
					reject();
				},
				fail: function(e) {
					reject();
				}
			});
		})
}

